<template>
  <work-template v-model="detailForm" @submitOn="submitOn"
                 @closeSelectedTag="closeSelectedTag" :butShow="true">
    <div slot="form" class="container-div">
      <GPSWarning v-model="detailForm" ref="refGPSWarning"/>
    </div>
  </work-template>
</template>

<script>
import * as apis from "@/api/warning"

export default {
  name: '',
  components: {
    GPSWarning: () => import('./components/GPSWarning.vue')
  },
  computed: {},
  watch: {},
  data() {
    return {
      detailForm: {
        rows: [
          {warnType: "1", configSwitch: '', warnSustainDays: ''},
          {
            warnType: "2",
            warnSonType: '',
            warnSonTypeStr: '',
            configSwitch: '',
            warnSustainDays: '',
            earlyWarn: [],
            earlyWarnDays: '',
          },
          {
            warnType: "3",
            warnSonType: '',
            warnSonTypeStr: '',
            configSwitch: '',
            warnSustainDays: '',
            earlyWarn: [],
            earlyWarnDays: ''
          },
        ]
      }


    };
  },
  created() {
    this.handleList()
  },
  mounted() {
  },
  methods: {
    async handleList() {
      try {
        let res = await apis?.gpsWarnConfig()
        if (res.rows && res.rows.length > 0) {
          this.detailForm.rows = this.handleRow(res.rows)
        }


      } catch (e) {
        console.error(e)
      }
    },

    handleRow(list) {
      list.forEach(item => {
        if (item.warnType !== '1') {
          item.earlyWarn = JSON.parse(item.earlyWarn)
        }
      })
      return list
    },

    async submitOn() {
      let loading = null
      try {
        await this.$refs.refGPSWarning.handleEffect()
        loading = this.$loading({
          lock: true,
          text: "保存中",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)",
        });
        let res = await apis?.gpsWarnConfigPost(this.detailForm.rows)
        if (res.code === 200) {
          this.$message.success('修改成功')
          this.handleList()
        }

      } catch (e) {
        console.error(e)
      } finally {
        loading?.close();
      }
    },

    closeSelectedTag() {
      this.$tab.refreshPage();
      this.$modal.msgSuccess('取消成功')
    }
  },
};
</script>

<style scoped lang=""></style>


